import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

// 组件异步加载
// 评论组件
const Recommend = (resolve) => {
    import('components/recommend/recommend').then((recommend) => {
        resolve(recommend)
    })
}
const Disc = (resolve) => {
    import('components/disc/disc').then((disc) => {
        resolve(disc)
    })
}

// 歌手组件
const Singer = (resolve) => {
    import('components/singer/singer').then((singer) => {
        resolve(singer)
    })
}
const SingrtDetail = (resolve) => {
    import('components/singer-detail/singer-detail').then((singrtDetail) => {
        resolve(singrtDetail)
    })
}

// 排行组件
const Rank = (resolve) => {
    import('components/rank/rank').then((rank) => {
        resolve(rank)
    })
}
const TopList = (resolve) => {
    import('components/top-list/top-list').then((topList) => {
        resolve(topList)
    })
}

// 搜索组件
const Search = (resolve) => {
    import('components/search/search').then((search) => {
        resolve(search)
    })
}

// 个人中心
const Mine = (resolve) => {
    import('components/mine/mine').then((mine) => {
        resolve(mine)
    })
}

const router = new Router({
    routes: [
        {
            // 为根目录配置默认路径
            // 重定向到 /recommend
            path: '/',
            redirect: '/recommend'
        },
        {
            path: '/recommend',
            component: Recommend,
            children: [
                {
                    path: ':id',
                    component: Disc

                }
            ]
        },
        {
            path: '/singer',
            component: Singer,
            children: [
                {
                    path: ':id',
                    component: SingrtDetail

                }
            ]
        },
        {
            path: '/rank',
            component: Rank,
            children: [
                {
                    path: ':id',
                    component: TopList
                }
            ]
        },
        {
            path: '/mine',
            component: Mine
        },
        {
            path: '/search',
            component: Search
        }
    ]
})

// 不许直接进入除首页外页面
router.beforeEach((to, from, next) => {
    from.path === '/' && to.path !== '/recommend'
        ? next('/')
        : next()
})

export default router
